package server;

import container.Host;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

//使用这个对象去解析请求
public class RequestProcessorNew extends Thread {

    private Socket socket;
    private Host host;

    public RequestProcessorNew(Socket socket, Host host) {
        this.socket = socket;
        this.host = host;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            Object resolver = host.getResolver(request.getUrl());

            if(resolver instanceof HttpServlet){
                ((HttpServlet)resolver).service(request,response);
            }else{
                response.outputHtml(resolver.toString(),true);
            }

            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
